package com.triones.ljr.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.BannerDO;
import com.triones.ljr.entity.dto.WxUserDO;
import com.triones.ljr.entity.vo.WxUserVO;
import com.triones.ljr.mapper.WxUserMapper;
import com.triones.ljr.service.WxUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 微信用户
 */
@Service
@RequiredArgsConstructor
public class WxUserServiceImpl implements WxUserService {

    private final WxUserMapper wxUserMapper;

    @Override
    public TableDataInfo<WxUserVO> pageList(WxUserDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<WxUserDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(WxUserDO::getIsDel, 0)
            .and(StringUtils.isNotBlank(dto.getNickName()),
                wq -> wq.like(WxUserDO::getNickName, dto.getNickName()).or()
                    .like(WxUserDO::getRealName, dto.getNickName()).or()
                    .like(WxUserDO::getPhone, dto.getNickName())
            )
            .eq(StringUtils.isNotBlank(dto.getSex()), WxUserDO::getSex, dto.getSex())
            .eq(StringUtils.isNotBlank(dto.getStatus()), WxUserDO::getStatus, dto.getStatus())
            .eq(StringUtils.isNotBlank(dto.getTypeMark()), WxUserDO::getTypeMark, dto.getTypeMark())
            .orderByAsc(WxUserDO::getCreateTime);
        Page<WxUserVO> result = wxUserMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean updateStatus(WxUserDO dto) {
        WxUserDO wxUserDO = wxUserMapper.selectById(dto.getId());
        if (wxUserDO != null) {
            wxUserDO.setStatus(dto.getStatus());
            return wxUserMapper.updateById(wxUserDO) > 0;
        }
        return false;
    }

    @Override
    public boolean pwdReset(WxUserDO dto) {
        if (StringUtils.isBlank(dto.getPassword())) {
            throw new RuntimeException("密码不能为空");
        }
        WxUserDO wxUserDO = wxUserMapper.selectById(dto.getId());
        if (wxUserDO == null) {
            throw new RuntimeException("未查询到用户信息");
        }
        wxUserDO.setPassword(BCrypt.hashpw(dto.getPassword()));
        return wxUserMapper.updateById(wxUserDO) > 0;
    }

    @Override
    public WxUserDO getWxUserByOpenid(String openid) {
        return wxUserMapper.selectOne(new LambdaQueryWrapper<WxUserDO>()
            .eq(WxUserDO::getOpenid, openid)
            .eq(WxUserDO::getIsDel, 0));
    }

    @Override
    public WxUserDO add(WxUserDO wxUserDO) {
        wxUserMapper.insert(wxUserDO);
        return wxUserDO;
    }

    @Override
    public WxUserVO getWxUserById() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        WxUserVO wxUserVO = wxUserMapper.selectVoById(loginUser.getUserId());
        if (wxUserVO == null) {
            throw new ServiceException("当前用户未注册!");
        }
        if (wxUserVO.getStatus().equals("2")) {
            throw new ServiceException("当前用户已被禁用!");
        }
        return wxUserVO;
    }

    @Override
    public WxUserDO bindPhone(WxUserVO wxUser, String phoneNumber) {
        WxUserDO wxUserDO = wxUserMapper.selectById(wxUser.getId());
        if (wxUserDO == null) {
            throw new ServiceException("未查询到有效的用户信息！");
        }
        wxUserDO.setPhone(phoneNumber);
        wxUserMapper.updateById(wxUserDO);
        return wxUserDO;
    }
}
